<?php
$ERROR="";
function GetBasePrice($string)
{
	$query = "SELECT price FROM works WHERE name = '".$string."'";
	require_once("./includes/class.FetchFromMySQL.inc.php");
	$sql = new FetchFromMySQL("localhost", "szofttech", "alma");
	$sql->select_db("szofttech");
	$result = $sql->query($query);
	$result = $sql->FetchAsObject();
	if ( $sql->AffectedRows() != 0 ) return $result->price;
	return 0;
}

function CatchInput($fieldname)
{
	if ( !empty($_POST[$fieldname]) && is_numeric($_POST[$fieldname]) ) 
	//&& is_numeric($_POST['kvs']) && is_numeric($_POST['elox']) && is_numeric($_POST['paint']) )
	{
		// echo "feltolt";
		$query= 'SELECT name FROM works WHERE name = \''.$fieldname.'\'';
		require_once("./includes/class.MySQL.inc.php");
		$sql = new MySQL("localhost", "szofttech", "alma");
		//$sql->ConnectionTest();
		$sql->select_db("szofttech");
		$sql->query($query);
		if ( $sql->AffectedRows() != 0 )
		{
			// echo "UPDATE";
			$query = 'UPDATE `works` SET `price` = \''.$_POST[$fieldname].'\' WHERE CONVERT(`works`.`name` USING utf8) = \''.$fieldname.'\' LIMIT 1;';
		}
		else
		{
			// echo "INSERT";
			$query = 'INSERT INTO `works` (`name`, `price`) VALUES (\''.$fieldname.'\', \''.$_POST[$fieldname].'\');';
		}
		$sql->query($query);
		return true;
	}
	elseif( empty($_POST[$fieldname]) )
	{
		//do nothing
	}
	else
	{
		// echo "Valami szar";
		$ERROR = "A ".$fieldname." szám kell legyen!";
	}
}





if ( $_POST['submit'] == "OK" && isset($_POST['standard']) && isset($_POST['kvs']) && isset($_POST['elox']) && isset($_POST['paint']))
{
	if (!$ERROR ) CatchInput("standard");
	if (!$ERROR ) CatchInput("kvs");
	if (!$ERROR ) CatchInput("elox");
	if (!$ERROR ) CatchInput("paint");	
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body  onload="document.forms[0].standard.focus();">
<p><strong>Alapadatok szerkesztése</strong></p>
<p>Itt módosíthatja az alapadatokhoz megadott árakat!</p>
<form name="default" method="post" action="">
<table width="600" border="1">
  <tr>
    <th width="287" scope="col">Megnevezés</th>
    <th width="116" scope="col">Jelenlegi ár</th>
    <th width="175" scope="col">Új ár</th>
  </tr>
  <tr>
    <td>&quot;Standard&quot; Költségek (ár/db)</td>
    <td><div align="center"><? echo GetBasePrice("standard");?></div></td>
    <td>
      <label>
        <input type="text" name="standard" id="standard" />
      </label>
    </td>
  </tr>
  <tr>
    <td>Eloxálás (ár/m2)</td>
    <td><div align="center"><? echo GetBasePrice("elox");?></div></td>
    <td>
      <label>
        <input type="text" name="elox" id="elox" />
      </label>
    </td>
  </tr>
  <tr>
    <td>KVS Bevonat (ár/m2)</td>
    <td><div align="center"><? echo GetBasePrice("kvs");?></div></td>
    <td>
      <label>
        <input type="text" name="kvs" id="kvs" />
      </label>
    </td>
  </tr>
  <tr>
    <td>Felfestés (ár/db)</td>
    <td><div align="center"><? echo GetBasePrice("paint");?></div></td>
    <td>
      <label>
        <input type="text" name="paint" />
      </label>
    </td>
  </tr>
</table>

<p>
  <label>
    <input type="submit" name="submit" id="OK" value="OK" />
  </label>
</p>
<? echo $ERROR; ?>
</form>
<p>&nbsp;</p>
</body>
</html>